package com.four.service.impl;

import com.four.entity.Admin;
import com.four.mapper.AdminMapper;
import com.four.service.AdminService;
import com.four.util.WebUtil;
import com.four.vo.AdminVo;
import com.four.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * ClassName:AdminService
 * Description:
 * date:2022/10/13 19:34
 *
 * @since JDK 1.8
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Result findByAccountAndPass(String account, String password) {
        Admin admin = adminMapper.selectByAccountAndPass(account, password);
        if (admin != null){
            WebUtil.getSession().setAttribute("admin",admin);
            return new Result(200,"登录成功");
        }
        return new Result(500,"登录失败");
    }

    @Override
    public Result qr(Integer adminId, Integer[] roleIds) {
        //1.根据用户删除该用户具有的角色id
        adminMapper.deleteByUserId(adminId);
        //2.添加新的角色id
        adminMapper.insertAdminId(adminId,roleIds);

        return new Result(200,"添加成功");
    }
    @Override
    public Result findByCondition(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Admin> admins = adminMapper.findByCondition();
        PageInfo<Admin> pageInfo = new PageInfo<>(admins);
        return new Result(200,"查询成功",pageInfo);
    }

    @Override
    public Result selectAdminMessageByid() {
        Admin admin = (Admin) WebUtil.getSession().getAttribute("admin");
        Admin admin1 = adminMapper.selectAdminmessage(admin.getId());
        return new Result(200,"查询成功",admin1);
    }

    @Override
    public Result update(Admin admin) {
        adminMapper.updateByPrimaryKey(admin);
        return new Result(200,"修改成功");
    }

    @Override
    public Result addAdmin(Admin admin) {
        int insert = adminMapper.insert(admin);
        return new Result(200,"添加成功",insert);
    }

    @Override
    public Result updatePassword(AdminVo adminVo) {
        adminMapper.updateByPass(adminVo);
        return new Result(200,"修改成功");

    }

    @Override
    public Result delete(Integer id) {
        adminMapper.deleteByAdmin(id);
        return new Result(200,"删除成功");
    }
}
